/*****************************************
* require   jquery, jquery.fancybox, jquery.cookie
* require   notice.dock.css
* name     
* author    zolunX10@hotmail.com
* description
* version 1.0 2011-5-10
*******************************************/

/****
* init the dock DOM
*/
window.NoticeDock=(function($){
  return function(settings){
    var config={
      uid: null,
      username: null
    };
    if (settings) $.extend(config, settings);
    this.$dock= null;
    this.$box= null;
    this.initDock(config);
    this.initBox();
    
    if (null== config.uid || null== config.username){
      this.initGuest();
    } else{
      this.initLogged();
    }
  }
})(jQuery);

NoticeDock.prototype={
  //各种初始化函数
  initGuest: function(){
    if (! this.$dock){
      return;
    }
    this.initAlert();
  },
  initLogged: function(uid, uname){
    if (! this.$dock){
      return;
    }
    this.initAlert();
    this.$pm= this.$dock.find('#dock_pm');
  },
  initAlert: function(){
    var that= this;
    this.$alert= this.$dock.find('#dock_alert');
    this.$alert.fancybox({
      content: that.$box,
      onClosed: function(){
        that.clearAlert();
      }
    });
  },
  initDock: function(config){
    if (! this.$dock){
      this.$dock=$('#dock');
      if (this.$dock.length<=0){
        this.$dock=$(document).append('<div id="dock">');
      }
    }
    return this.$dock;
  },
  initBox: function(){
    var $box=this.$box;
    $box= $('#dock_box');
    if ($box.length <=0){
      $box= $('<div id="dock_box">').width(500);
      $box.append('<h3>系统消息</h3>');
      $box.$ul= $('<ul>').appendTo($box);
      $box.clear= function(){
        $box.$ul.empty();
      }
      $box.add= function(data){
        $('<li>').html(data).appendTo(this.$ul);
      }
    }
    this.$box= $box;
    //this.$box.show();
    return this.$box; 
  },
  //系统消息
  addAlert: function(data){
    this.$box.add(data);
    this.$alert.addClass('highlight');
  },
  clearAlert: function(){
    this.$box.clear();
    this.$alert.removeClass('highlight');
  },
  showAlert: function(){
    this.$alert.click();
  },
  
  //短信箱
  showPM: function(){
    //ajax获取UCenter的短信
    //TODO
  },
  addPM: function(data){
    this.$pm.addClass('highlight');
  }
};
NoticeDock.that= null;
NoticeDock.get= function(settings){
  if (!NoticeDock.that){
    NoticeDock.that= new NoticeDock(settings);
  }
  return NoticeDock.that;
}